package com.example.mychat.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.example.mychat.Bean.Chatitem;
import com.example.mychat.R;

import java.text.SimpleDateFormat;
import java.util.List;

public class ChatitemAdapter extends ArrayAdapter<Chatitem> {
    int resourceid;
    public ChatitemAdapter(@NonNull Context context, int resource, @NonNull List<Chatitem> objects) {
        super(context, resource, objects);
        this.resourceid=resource;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        View view=convertView;
        if (view==null){
            view= LayoutInflater.from(getContext()).inflate(resourceid,null);
        }
        ImageView userimg=view.findViewById(R.id.userimg);
        TextView username=view.findViewById(R.id.username);
        TextView lastcontent=view.findViewById(R.id.lastcontent);
        TextView lastdate=view.findViewById(R.id.lastdate);
        Chatitem c=getItem(position);
        userimg.setImageResource(c.getUserimg());
        username.setText(c.getUsername());
        lastcontent.setText(c.getLastcontent());
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String newdata=sdf.format(c.getLastdate());
        lastdate.setText(newdata);
        return  view;
    }
}
